package com.gs.SynAntiWandering.util;

import static android.app.Notification.VISIBILITY_PUBLIC;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.gs.SynAntiWandering.R;


public class NotificationUtil {
    private static final String CHANNEL_ID = "10000";
    private static final String CHANNEL_FOREGROUND_ID = "SynAntiWandering_channel_foreground";
    private static final String CHANNEL_NAME = AppUtil.getInstance().getContext().getResources().getString(R.string.work_order_notify);
    private static final String CHANNEL_FOREGROUND_NAME = AppUtil.getInstance().getContext().getResources().getString(R.string.service);
    private NotificationManager manager;
    private static volatile NotificationUtil instance;

    public static NotificationUtil getInstance() {
        if (instance == null) {
            synchronized (NotificationUtil.class) {
                if (instance == null) {
                    instance = new NotificationUtil();
                }
            }
        }
        return instance;
    }

    public NotificationManager getNotificationManager() {
        if (manager == null) {
            manager = (NotificationManager) AppUtil.getInstance().getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public NotificationChannel getChannel() {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.setBypassDnd(true); // 绕过请勿打扰模式
        channel.setShowBadge(true); // 桌面launcher消息角标
        channel.setLightColor(Color.RED); // 闪光时的灯光颜色
        channel.enableLights(true); // 闪光
        channel.setVibrationPattern(new long[]{100, 100, 200}); // 振动频率
        channel.enableVibration(true); // 振动
        channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        channel.setLockscreenVisibility(VISIBILITY_PUBLIC);
        return channel;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public NotificationChannel getForegroundChannel() {
        NotificationChannel channel = new NotificationChannel(CHANNEL_FOREGROUND_ID, CHANNEL_FOREGROUND_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.setBypassDnd(true); // 绕过请勿打扰模式
        channel.setShowBadge(false); // 桌面launcher消息角标
        channel.enableLights(false); // 闪光
        channel.enableVibration(false); // 振动
        channel.setSound(null, null);
        channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        return channel;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getNotification(Context context) {
        return new Notification.Builder(context, CHANNEL_ID)
                .setPriority(Notification.PRIORITY_MAX)
                .setOnlyAlertOnce(true);
    }

    public NotificationCompat.Builder getNotificationCompat(Context context) {
        return new NotificationCompat.Builder(context, CHANNEL_ID)
                .setOnlyAlertOnce(true);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getForegroundNotification(Context context) {
        return new Notification.Builder(context, CHANNEL_FOREGROUND_ID)
                .setOnlyAlertOnce(true);
    }

    public NotificationCompat.Builder getForegroundNotificationCompat(Context context) {
        return new NotificationCompat.Builder(context, CHANNEL_FOREGROUND_ID)
                .setOnlyAlertOnce(true);
    }
}
